package com.example.netcenter;

import android.app.ProgressDialog;
import android.content.Context;

import com.example.netcenter.Event.Response;
import com.example.netcenter.bean.ErrorBean;

import com.example.utilcenter.alertUtil.ToastUtil;
import com.google.gson.Gson;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class NetCenter<T> {

    private Context mContext = null;

    public NetCenter(Context context) {
        mContext = context;
    }

    public NetCenter() {

    }

    /**
     * 没有加载的请求
     *
     * @param tObservable
     * @param response
     */
    @SuppressWarnings("unchecked")
    public void GetNetCenter(Observable<T> tObservable, final Response response) {

        tObservable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<T>() {
                    @Override
                    public void call(T bean) {
                        if (response != null) {
                            response.OK(bean);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                        if (response != null) {


                            if (mContext != null) {

                                if (!throwable.getLocalizedMessage().contains("100000") && throwable.getLocalizedMessage().contains("Code")) {

                                    ErrorBean errorBean = new Gson().fromJson(throwable.getMessage(), ErrorBean.class);

                                    ToastUtil.showSingleToast(mContext, errorBean.getMsg());

                                }
                            }

                            response.Fail(throwable);

                        }
                    }
                }, new Action0() {
                    @Override
                    public void call() {
                        if (response != null) {
                            response.complete();
                        }
                    }
                });

    }


    /**
     * 附带加载的请求
     *
     * @param context
     * @param tObservable
     * @param response
     */

    @SuppressWarnings("unchecked")
    public void GetNetCenter(final Context context, Observable<T> tObservable, final Response response) {

        final ProgressDialog dialog = ProgressDialog.show(context, "", "努力加载中...");

        tObservable
                .subscribeOn(Schedulers.io())

                .observeOn(AndroidSchedulers.mainThread())

                .subscribe(new Action1<T>() {
                    @Override
                    public void call(T bean) {

                        if (response != null) {

                            response.OK(bean);

                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                        if (response != null) {

                            if (!throwable.getLocalizedMessage().contains("100000") && throwable.getLocalizedMessage().contains("Code")) {

                                ErrorBean errorBean = new Gson().fromJson(throwable.getMessage(), ErrorBean.class);

                                ToastUtil.showSingleToast(context, errorBean.getMsg());

                            }
                            response.Fail(throwable);

                            if (dialog.isShowing()) {

                                dialog.dismiss();

                            }
                        }
                    }
                }, new Action0() {
                    @Override
                    public void call() {

                        if (response != null) {

                            if (dialog.isShowing()) {

                                dialog.dismiss();

                            }
                            response.complete();

                        }
                    }
                });

    }


}